<?php
namespace app\admin\model;
use \think\Session;
use think\Db;
class User extends BaseModel
{
    protected $table = 'cz_admin';
    protected $rule = [
        'uid'  =>  '',
    ];
    protected $msg = [
        'uid'  =>  '',
    ];
    protected $return = array();
    /**
     * 系统用户登录
     *
     * @param unknown $user_name
     * @param unknown $password
     */
    public function login($user_name, $password = '')
    {
        $condition = array(
            'username' => $user_name
        );
        $user_info = $this->getInfo($condition, $field = '*');
        if (! empty($user_info)) {
            if($password == authcode($user_info['password'],'DECODE')){
                if ($user_info['status'] == 0) {
                    return -3;
                } else {
                    Session::set('adminId', $user_info['id']);
                    return 1;
                }
            }else{
                return -1;
            }
        } else
            return -2;
    }
    //获取用户信息
    public function getList($id){
        $where = array();
        $where ['id'] = $id;
        $userInfo = $this->getInfo($where);
        return $userInfo;
    }
    public function checkPwd($data){
        $condition = array(
            'id' => Session::get("adminId")
        );
        $user_info = $this->getInfo($condition, $field = '*');
        if (authcode($user_info['password'], 'DECODE') != $data['old_password']) {
            $this->return ['status'] = 0;
            $this->return ['msg'] = '原密码输入错误';
        } else {
            $data ['password'] = authcode($_POST['new_password'], 'ENCODE');
            $res = $this->save($data,$condition);
            if($res){
                $this->return ['status'] = 1;
                $this->return ['msg'] = '修改成功';
            }else{
                $this->return ['status'] = 0;
                $this->return ['msg'] = '修改失败';
            }
        }
        return $this->return;
    }
}
